package com.xiaojian.pick.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaojian.pick.entity.Support;
import com.xiaojian.pick.mapper.SupportMapper;
import com.xiaojian.pick.service.CommodityService;
import com.xiaojian.pick.service.SupportService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author whycode
 * @title: SupportServiceImpl
 * @projectName campus-secondary-market-app---sys
 * @description: TODO
 * @date 2022/3/2615:56
 */
@Service
public class SupportServiceImpl extends ServiceImpl<SupportMapper, Support> implements SupportService {

    @Resource
    @Lazy
    private CommodityService commodityService;
    @Override
    public boolean save(Support entity) {
        boolean save = super.save(entity);
        if(save){
            commodityService.increaseSupport(entity.getCommodityId());
        }
        return save;
    }

    @Override
    public void delete(Support support) {
        boolean remove = this.remove(new QueryWrapper<Support>().eq("c_id", support.getCommodityId())
                .eq("u_id", support.getUserId()));

        if(remove){
            commodityService.decreaseSupport(support.getCommodityId());
        }
    }
}
